package com.kuqi.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kuqi.blog.dao.mapper.CommentMapper;
import com.kuqi.blog.dao.pojo.Comment;
import com.kuqi.blog.dao.pojo.SysUser;
import com.kuqi.blog.service.CommentsService;
import com.kuqi.blog.service.SysUserService;
import com.kuqi.blog.util.UserThreadLocal;
import com.kuqi.blog.vo.CommentVo;
import com.kuqi.blog.vo.Result;
import com.kuqi.blog.vo.UserVo;
import com.kuqi.blog.vo.params.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:kuqi
 * @createTime:2021/12/17 10:25
 * @description:
 */

@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentMapper commentMapper;

    //用来调用查询作者信息，让后存储到CommentVo中
    @Autowired
    private SysUserService sysUserService;

    @Override
    public Result commentsByArticleId(Long articleId) {

        /**
         * 1.根据文章id查询评论列表，从comment表中查询
         * 2.根据作者的id查询作者的信息
         * 3.判断如果level = 1要去查询它有没有子评论
         * 4.如果有根据评论id进行查询（parent_id）
         */
        //相当于 from comment
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //根据文章id查询相当于 where id=#{articleId}
        queryWrapper.eq(Comment::getArticleId,articleId);
        //根据层级关系进行查询  and level = #{level}
        queryWrapper.eq(Comment::getLevel,1);
        List<Comment> comments = commentMapper.selectList(queryWrapper);

        List<CommentVo> commentVoList = copyList(comments);
        return Result.success(commentVoList);
    }

    @Override
    public Result comment(CommentParam commentParam) {
        //在当前线程的线程池中拿到当前用户的信息
        SysUser sysUser = UserThreadLocal.get();
        //创建一个新的评论实体类对象
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        //用户id从当前线程池中的用户信息获取
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParam.getParent();
        if (parent == null || parent == 0){
            comment.setLevel(1);
        }else {
            comment.setLevel(2);
        }
        //如果是空，parent就是0
        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        this.commentMapper.insert(comment);
        return Result.success(null);
    }

    //对列表中的comment进行判断
    private List<CommentVo> copyList(List<Comment> comments) {
        //新建一个有序列表
        ArrayList<CommentVo> commentVoList = new ArrayList<>();
        //遍历comments列表，每一项都是一个comment对象;请其存储到commentVoList中
        for (Comment comment : comments) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    //将comment中的变量值赋值到commentVo中
    private CommentVo copy(Comment comment){
        //新建一个CommentVo对象
        CommentVo commentVo =  new CommentVo();
        //相同属性复制id,comment,createDate,level
        BeanUtils.copyProperties(comment,commentVo);

        /**
         * 作者信息
         */
        Long authorId = comment.getAuthorId();
        //查找用户信息的id,avatar,nickname
        UserVo userVo =this.sysUserService.findUserVoById(authorId);

        /**
         * 子评论
         */
        Integer level = comment.getLevel();
        if (1 == level){
            Long id = comment.getId();
            List<CommentVo> commentVoList = findCommentsByParentById(id);
            commentVo.setAuthor(userVo);
        }

        /**
         * to User 给谁评论
         */
        if (level > 1){
            Long toUid = comment.getToUid();
            UserVo  toUserVo = this.sysUserService.findUserVoById(toUid);
            commentVo.setToUser(toUserVo);
        }

        return commentVo;
    }

    /**
     * 根据父评论Id查询子评论的相关信息
     * @param id
     * @return
     */
    private List<CommentVo> findCommentsByParentById(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId,id);
        queryWrapper.eq(Comment::getLevel,2);
        List<Comment> comments = this.commentMapper.selectList(queryWrapper);
        return copyList(comments);
    }

}
